﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FTools.Dominio;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

namespace FTools.Persistencia
{
    class PerRubro
    {
        static string msg = "";

        //OBTENER RUBRO
        public Rubro obtenerRubro(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdRubro)
        {
            Rubro objRubro = new Rubro();
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select * from RUBRO where ID_RUBRO=@IdRubro");

                command.Parameters.AddWithValue("@IdRubro", xIdRubro);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];

                    objRubro.IdRubro = (long)fila.ItemArray[0];
                    objRubro.Nombre = (String)fila.ItemArray[1]; objRubro.Nombre = objRubro.Nombre.Trim();
                }
                return objRubro;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE RUBRO
        public Boolean existeRubro(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdRubro)
        {
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from RUBRO where ID_RUBRO=@IdRubro");

                command.Parameters.AddWithValue("@IdRubro", xIdRubro);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE RUBRO
        public Boolean existeRubro(SqlConnection xConexion, SqlTransaction xTransaccion, String xNombre)
        {
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from RUBRO where NOMBRE=@Nombre");

                command.Parameters.AddWithValue("@Nombre", xNombre.Trim());
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //OBTENER COLECCION Rubro
        public ArrayList obtenerColeccionRubro(SqlConnection xConexion, SqlTransaction xTransaccion)
        {
            SqlDataAdapter dataAdapter;
            DataTable dataTable = new DataTable();
            ArrayList colRubro = new ArrayList();

            try
            {
                SqlCommand command = new SqlCommand("select * from RUBRO");

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Rubro objRubro = new Rubro();
                    DataRow fila = dataTable.Rows[i];
                    objRubro.IdRubro = (long)fila.ItemArray[0];
                    objRubro.Nombre = (String)fila.ItemArray[1]; objRubro.Nombre = objRubro.Nombre.Trim();
                    colRubro.Add(objRubro);
                }
                return colRubro;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //ALTA Rubro
        public int altaRubro(SqlConnection xConexion, SqlTransaction xTransaccion, Rubro xObjRubro)
        {
            try
            {
                SqlCommand command = new SqlCommand("insert into RUBRO values (@IdRubro, @Nombre)");

                command.Parameters.AddWithValue("@IdRubro", xObjRubro.IdRubro);
                command.Parameters.AddWithValue("@Nombre", xObjRubro.Nombre.Trim());

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //BAJA Rubro
        public int bajaRubro(SqlConnection xConexion, SqlTransaction xTransaccion, Rubro xObjRubro)
        {
            try
            {
                SqlCommand command = new SqlCommand("delete from RUBRO where ID_RUBRO=@IdRubro");

                command.Parameters.AddWithValue("@IdRubro", xObjRubro.IdRubro);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //MODIFICA Rubro
        public int modificaRubro(SqlConnection xConexion, SqlTransaction xTransaccion, Rubro xObjRubro)
        {
            try
            {
                SqlCommand command = new SqlCommand("update RUBRO set NOMBRE=@Nombre " +
                                                    "where ID_RUBRO=@IdRubro");

                command.Parameters.AddWithValue("@IdRubro", xObjRubro.IdRubro);
                command.Parameters.AddWithValue("@Nombre", xObjRubro.Nombre.Trim());

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }
    }
}
